package tree

const head = 'a'

type TrieNode struct {
	char     uint8
	children [26]*TrieNode
	EndChar  bool
}

func NewTrieNode(char uint8) *TrieNode {
	return &TrieNode{
		char:     char,
		children: [26]*TrieNode{},
	}
}

type Trie struct {
	root *TrieNode
}

func NewTrie() *Trie {
	return &Trie{root: NewTrieNode('/')}
}

func (t *Trie) Insert(text string) {
	root := t.root
	for i := 0; i < len(text); i++ {
		char := text[i]
		index := char - head

		if root.children[index] == nil {
			root.children[index] = NewTrieNode(char)
		}
		root = root.children[index]
	}
	root.EndChar = true

}

func (t *Trie) find(pattern string) bool {
	root := t.root
	for i := 0; i < len(pattern); i++ {
		index := pattern[i] - head
		if root.children[index] == nil {
			return false
		}
		root = root.children[index]
	}
	return root.EndChar
}
